<script>

export default {
  name: "register",
  data() {
    // 重复校验密码
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.user.password) {
        callback(new Error('二次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      user: {
        username: '',
        password: '',
        confirmPass: '' //表单里用户输入的code
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ], password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 14, message: '长度在 6 到 14 个字符', trigger: 'blur'}
        ],
        confirmPass: [
          {validator: validatePassword, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    register() {
      this.$refs['registerRef'].validate(valid => {
        if (valid) {
          //验证通过
          this.$request.post('/register', this.user).then(res => {
            console.log(res);
            if (res.code === "200") {
              this.$message.success(res.data.username + "用户 注册成功!")
              this.$router.push('/login')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<template>
  <div class="register">
    <div class="box">
      <div style="flex:1">
        <img src="@/assets/register.jpg" alt="" style="width: 500px; height: 500px; background-size: cover;
        background-position: center;background-repeat: no-repeat">
      </div>
      <div style="flex:1;display: flex; align-items: center;justify-content: center">
        <el-form :model="user" style="width: 80%" ref="registerRef" :rules="rules">
          <div class="title">欢迎注册后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="large" id="username" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="large" show-password id="password" placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPass">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-lock" size="large" show-password style="flex: 1" placeholder="请确认密码"
                        v-model="user.confirmPass">
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="success" plain size="medium" style="width: 100%" @click="register">注册</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex: 1">
              <span>已有账号？请</span>
              <span style="color: #0f9876;cursor: pointer;user-select: none"
                    @click="$router.push('/login')">点击登录</span>
            </div>
            <div style="flex: 1;text-align: right;color: #0f6898;cursor: pointer">忘记密码</div>
          </div>
        </el-form>
      </div>
    </div>
  </div>

</template>

<style scoped>
.register {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0f9876;
}

.box {
  display: flex;
  background-color: white;
  width: 60%;
  height: 500px;
  border-radius: 5px;
  overflow: hidden;
}

.title {
  font-size: 30px;
  color: #000;
  font-weight: bold;
  text-align: center;
  padding-bottom: 20px;
}

</style>